//
//  ChatModel.swift
//  WalkNovel
//
//  Created by Kevin on 19/2/2023.
//

import UIKit
import HandyJSON

struct ChatModesModel: HandyJSON {
    var object: String?
    var data: [ChatModel]?
    var error: ChatErrorModel?
}

struct ChatModel: HandyJSON {
    var id: String?
    var object: String?
    var created: Int?
    var model: String?
    var error: ChatErrorModel?
    
    var choices: [ChatChoicesModel]?
    var usage: ChatUsageModel?
    
    var owned_by: String?
    var permission: [ChatPermissionModel]?
    var root: String?
    var parent: String?
    
    var data: [ChatImageModel]?
    
    var text: String?
}

struct ChatChoicesModel: HandyJSON {
    var text: String?
    var index: Int?
    var logprobs: String?
    var finish_reason: String?
    var message: ChatMessageModel?
}

struct ChatMessageModel: HandyJSON {
    var role: String?
    var content: String?
    var total_tokens: Int?
}

struct ChatUsageModel: HandyJSON {
    var prompt_tokens: Int?
    var completion_tokens: Int?
    var total_tokens: Int?
}

struct ChatPermissionModel: HandyJSON {
    var id: String?
    var object: String?
    var created: Int?
    var allow_create_engine: Bool = false
    var allow_sampling: Bool = false
    var allow_logprobs: Bool = false
    var allow_search_indices: Bool = false
    var allow_view: Bool = false
    var allow_fine_tuning: Bool = false
    var organization: String?
    var group: Int?
    var is_blocking: Bool = false
}

struct ChatImageModel: HandyJSON {
    var url: String?
}

struct ChatErrorModel: HandyJSON {
    var message: String?
    var type: String?
    var param: String?
    var code: String?
}

struct LotteryModel: HandyJSON {
    var id: Int = 0
    var wheelName: String?
    var gold: Int = 0
    var image: String?
    var begin: Float = 0.0
    var end: Float = 0.0
    
    var userId: String?
    var name: String?
    var avatar: String?
    var status: Int = 0
    var cost: Int = 0
    var time: String?

    mutating func didFinishMapping() {
        if gold == 0 {
            image = "coin_0"
            begin = -22.5
            end = 22.5
        }else if gold == 10 {
            image = "coin_1"
            begin = 22.5
            end = 67.5
        }else if gold == 20 {
            image = "coin_2"
            begin = 67.5
            end = 112.5
        }else if gold == 30 {
            image = "coin_3"
            begin = 112.5
            end = 157.5
        }else if gold == 50 {
            image = "coin_4"
            begin = 157.5
            end = 201.5
        }else if gold == 100 {
            image = "coin_5"
            begin = 201.5
            end = 247.5
        }else if gold == 200 {
            image = "coin_6"
            begin = 247.5
            end = 292.5
        }else if gold == 500 {
            image = "coin_6"
            begin = 292.5
            end = 337.5
        }
    }
}

struct MessageListModel: HandyJSON {
    var list: [MessageModel]?
}

struct MessageModel: HandyJSON {
    var id: String?
    var type: Int = 0  // 1系统 2书籍
    var ctime: String?
    
    var title: String?
    var content: String?
    var imgUrl: String?
    
    var bookId: String?
    var name: String?
    var des: String?
    var author: String?
    var coverUrl: String?
    
    var allH: CGFloat = 0
    
    mutating func didFinishMapping() {
        if type == 1 {
            if let t = title {
                allH += wn_getTextH(textStr: t, font: .wn_font(name: .bold, size: 15), width: ScreenW-80, lineSpace: 1)+15
            }
            if let c = content {
                allH += wn_getTextH(textStr: c, font: .wn_font(name: .regular, size: 13), width: ScreenW-80)+6
            }
            if (imgUrl != nil && imgUrl?.count != 0) {
                allH += ((ScreenW-80)*ScreenW/375.0)*0.302+20
            }
            allH += 65
        }else if type == 2 {
            if let t = title {
                allH += (90*ScreenW/375.0)+94+wn_getTextH(textStr: t, font: .wn_font(name: .bold, size: 15), width: ScreenW-80)
            }else {
                allH += (90*ScreenW/375.0)+70
            }
        }
    }
}

struct ADModel: HandyJSON {
    var advertName: String?
    var articleId: Int = 0
    var jumpUrl: String?
    var imgUrl: String?
    var bookId: String?
    var author: String?
    var des: String?
    var coverUrl: String?
}
